package com.wangdao.mall.controller.client;

import com.google.gson.Gson;
import com.wangdao.mall.model.Result;
import com.wangdao.mall.model.Type;
import com.wangdao.mall.service.IndexService;
import com.wangdao.mall.service.IndexServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 21:56 2020/5/5
 * @Modified By:
 */
@WebServlet("/api/mall/index/*")
public class IndexServlet extends HttpServlet {

    private IndexService indexService = new IndexServiceImp();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/mall/index/", "");
        if ("getType".equals(action)){
            getType(request,response);
        }
    }

    /**
     * 获取商品类目
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = indexService.getType();
        Result result = new Result();
        result.setCode(0);
        result.setData(typeList);
        response.getWriter().println(gson.toJson(result));
    }
}
